package com.example.dao.impl;

import com.example.dao.ContactRecordDao;
import com.example.entity.customerAdmin.ContactRecord;
import com.example.utils.BaseDao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ContactRecordDaoImpl implements ContactRecordDao {


    @Override
    public List<ContactRecord> selectAll (int page, int limit) {
        List<ContactRecord> contactRecordList=new ArrayList<> ();
        Object[] objects=new Object[2];
        objects[0]=page;
        objects[1]=limit;
        ResultSet select = BaseDao.select ("select * from contact_record limit ?,?", objects);
        try {
            while (select.next ()){
                ContactRecord contactRecord=new ContactRecord ();
                contactRecord.setId (select.getInt ("id"));
                contactRecord.setCallPhone (select.getString ("call_phone"));
                contactRecord.setTime (select.getString ("time"));
                contactRecord.setType (select.getString ("type"));
                contactRecord.setCallObject (select.getString ("call_object"));
                contactRecord.setPhone (select.getString ("phone"));
                contactRecord.setCallDuration (select.getString ("call_duration"));
                contactRecordList.add (contactRecord);
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return contactRecordList;
    }

    @Override
    public int delete (int id) {
        int i=-1;
        Object[] objects=new Object[1];
        objects[0]=id;
        i = BaseDao.modify ("delete from contact_record where id=?", objects);

        return i;
    }

    @Override
    public ContactRecord selectId (int id) {
        ContactRecord contactRecord=new ContactRecord ();
        Object[] objects=new Object[1];
        objects[0]=id;
        ResultSet select = BaseDao.select ("select * from contact_record where id=?", objects);
        try {
            while (select.next ()){
                contactRecord.setId (select.getInt ("id"));
                contactRecord.setCallPhone (select.getString ("call_phone"));
                contactRecord.setTime (select.getString ("time"));
                contactRecord.setType (select.getString ("type"));
                contactRecord.setCallObject (select.getString ("call_object"));
                contactRecord.setPhone (select.getString ("phone"));
                contactRecord.setCallDuration (select.getString ("call_duration"));
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return contactRecord;
    }

    @Override
    public int getCount () {
        int i=-1;
        ResultSet select = BaseDao.select ("select count(1) getCount from contact_record ", null);
        try {
            while (select.next ()){
                i=select.getInt ("getCount");
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }
}
